package cn.yhbk.web.modules.manager.Service.Impl;

import cn.yhbk.web.common.Util.MyUtil;
import cn.yhbk.web.common.exception.ApiException;
import cn.yhbk.web.modules.manager.Pojo.Entity.*;
import cn.yhbk.web.modules.manager.Mapper.BursaryRecordMapper;
import cn.yhbk.web.modules.manager.Pojo.VO.Apply.ApplyResp;
import cn.yhbk.web.modules.manager.Pojo.VO.BursaryRecord.BursaryRecordExcel;
import cn.yhbk.web.modules.manager.Pojo.VO.OrderResponse;
import cn.yhbk.web.modules.manager.Service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 有何不可
 * @since 2024-02-21
 */
@Service
public class BursaryRecordServiceImpl extends ServiceImpl<BursaryRecordMapper, BursaryRecord> implements IBursaryRecordService {

    @Autowired
    private ITbApplyService applyService;

    @Autowired
    private ITbScholarshipService scholarshipService;

    @Autowired
    private ITbStudentService studentService;

    @Autowired
    private ITbGpaRecordService gpaRecordService;

    @Autowired
    private ITbClassService classService;

    @Override
    public boolean setRecord(Long bursaryId, Integer size,Long resultId) {
        List<ApplyResp> order = applyService.getOrderSort(resultId,bursaryId);

        //判断order为空
        if(order.isEmpty())throw new ApiException("排行为空");
        //获取奖学金信息
        TbScholarship scholarship = scholarshipService.getById(bursaryId);
        if(Objects.isNull(scholarship))throw new ApiException("该奖学金不存在!");

        List<BursaryRecord> resList = new ArrayList<>();

        for (int i = 0; i < size && i <order.size(); i++) {
            BursaryRecord record = new BursaryRecord();

            ApplyResp apply = order.get(i);

            Long stuId = apply.getStuId();
            TbStudent student = studentService.getById(stuId);
            if(Objects.isNull(student))throw new ApiException("该学生不存在!");

            record.setStuId(stuId);
            record.setBursaryId(bursaryId);
            record.setBursaryName(scholarship.getName());
            record.setOrder(i+1);

            record.setStuName(student.getName());

            TbClass tbClass = classService.getById(student.getClassId());

            String description = tbClass.getDescription();
            record.setStuDescription(description);


            TbGpaRecord gpaRecord = gpaRecordService.getRecordByStudentId(stuId);
            if(Objects.isNull(gpaRecord))throw new ApiException("该学生绩点记录不存在!");
            record.setGpaSum(gpaRecord.getGpaSum());

            record.setStuNumber(String.valueOf(student.getJobNumber()));

            String year = MyUtil.getCurrentYear();
            record.setYear(year);
            resList.add(record);
        }

        return saveBatch(resList);
    }

    @Override
    public List<BursaryRecord> getBursaryRecord(Long resultId, Long bursaryId) {
        List<Long> stuIds = studentService.getStuIdsByResultId(resultId);

        LambdaQueryWrapper<BursaryRecord> lambda = new QueryWrapper<BursaryRecord>().lambda();
        lambda.in(BursaryRecord::getStuId,stuIds);
        lambda.eq(BursaryRecord::getYear,MyUtil.getCurrentYear());
        if(Objects.isNull(bursaryId)){
            lambda.isNull(BursaryRecord::getBursaryId);
        }else{
            lambda.eq(BursaryRecord::getBursaryId,bursaryId);
        }

        lambda.orderByDesc(BursaryRecord::getGpaSum);

        return list(lambda);
    }

    @Override
    public boolean checkHasSet(Long resultId, Long bursaryId) {
        List<Long> stuIds = studentService.getStuIdsByResultId(resultId);
        LambdaQueryWrapper<BursaryRecord> lambda = new QueryWrapper<BursaryRecord>().lambda();

        lambda.in(BursaryRecord::getStuId,stuIds);
        lambda.eq(BursaryRecord::getBursaryId,bursaryId);
        lambda.eq(BursaryRecord::getYear,MyUtil.getCurrentYear());
        lambda.select(BursaryRecord::getId);

        return list(lambda).size() == 0;
    }

    @Override
    public boolean delBursaryRecord(Long resultId, Long bursaryId) {
        List<Long> stuIds = studentService.getStuIdsByResultId(resultId);

        LambdaQueryWrapper<BursaryRecord> lambda = new QueryWrapper<BursaryRecord>().lambda();

        lambda.in(BursaryRecord::getStuId,stuIds);
        lambda.eq(BursaryRecord::getBursaryId,bursaryId);
        lambda.eq(BursaryRecord::getYear,MyUtil.getCurrentYear());

        return remove(lambda);
    }

    @Override
    public boolean setRecord(Long resultId, Integer one, Integer two, Integer three) {
        List<Long> stuIds = studentService.getStuIdsByResultId(resultId);
        List<OrderResponse> orderResponses = gpaRecordService.getRecordByStudentIds(stuIds);
        List<ApplyResp> order = packageToApplyRes(orderResponses);
        System.out.println(order);

        //判断order为空
        if(order.isEmpty())throw new ApiException("排行为空");

        List<BursaryRecord> resList = new ArrayList<>();

        for (int i = 0; i < one && i <order.size(); i++) {
            ApplyResp apply = order.get(i);
            BursaryRecord record =packageRecord(apply,"校级一级奖学金",i);
            resList.add(record);
        }

        for(int i=one;i< (one+two) && i <order.size(); i++){
            ApplyResp apply = order.get(i);
            BursaryRecord record =packageRecord(apply,"校级二级奖学金",i);
            resList.add(record);
        }
        for(int i=one+two;i< (one+two+three) && i <order.size(); i++){
            ApplyResp apply = order.get(i);
            BursaryRecord record =packageRecord(apply,"校级三级奖学金",i);
            resList.add(record);
        }


        return saveBatch(resList);
    }

    @Override
    public List<BursaryRecordExcel> getBursaryRecordByYear(String year) {
        LambdaQueryWrapper<BursaryRecord> lambda = new QueryWrapper<BursaryRecord>().lambda();

        lambda.eq(BursaryRecord::getYear,year);

        return list(lambda).stream().map(item->{
            BursaryRecordExcel excelItem = new BursaryRecordExcel();
            BeanUtils.copyProperties(item,excelItem);
            return excelItem;
        }).collect(Collectors.toList());
    }

    private List<ApplyResp> packageToApplyRes(List<OrderResponse> orderResponses) {

        return orderResponses.stream().map(item->{
            ApplyResp applyResp = new ApplyResp();
            applyResp.setStuId(item.getStuId());
            applyResp.setGpaSum(item.getGpaSum());
            return applyResp;
        }).collect(Collectors.toList());
    }

    private BursaryRecord packageRecord(ApplyResp apply,String bursaryName,Integer i) {
        BursaryRecord record = new BursaryRecord();
        Long stuId = apply.getStuId();
        TbStudent student = studentService.getById(stuId);
        if(Objects.isNull(student))throw new ApiException("该学生不存在!");

        record.setStuId(stuId);
        record.setBursaryId(null);
        record.setBursaryName(bursaryName);
        record.setOrder(i+1);

        record.setStuName(student.getName());

        TbClass tbClass = classService.getById(student.getClassId());

        String description = tbClass.getDescription();
        record.setStuDescription(description);


        TbGpaRecord gpaRecord = gpaRecordService.getRecordByStudentId(stuId);
        if(Objects.isNull(gpaRecord))throw new ApiException("该学生绩点记录不存在!");
        record.setGpaSum(gpaRecord.getGpaSum());

        record.setStuNumber(String.valueOf(student.getJobNumber()));

        String year = MyUtil.getCurrentYear();
        record.setYear(year);
        return record;
    }


}
